<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>03-Less详解</title>
  
    
      <meta 
        property="og:title" 
        content="03-Less详解">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2009/08/01/09-%E7%A7%BB%E5%8A%A8Web%E5%BC%80%E5%8F%91/03-Less%E8%AF%A6%E8%A7%A3/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2009-08-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="移动web">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E9%A2%84%E5%A4%84%E7%90%86%E5%99%A8"><span class="toc-text">CSS 预处理器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E6%9C%89-CSS-%E9%A2%84%E5%A4%84%E7%90%86%E5%99%A8"><span class="toc-text">为什么要有 CSS 预处理器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF-CSS-%E9%A2%84%E5%A4%84%E7%90%86%E5%99%A8"><span class="toc-text">什么是 CSS 预处理器</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#less-%E7%9A%84%E4%BB%8B%E7%BB%8D"><span class="toc-text">less 的介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#less-%E7%9A%84%E8%AF%AD%E6%B3%95"><span class="toc-text">less 的语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E9%87%8A"><span class="toc-text">注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F"><span class="toc-text">定义变量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%B5%8C%E5%A5%97"><span class="toc-text">使用嵌套</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mixin"><span class="toc-text">Mixin</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Import"><span class="toc-text">Import</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0"><span class="toc-text">内置函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8-index-html%E4%B8%AD%E7%9B%B4%E6%8E%A5%E5%BC%95%E7%94%A8-less-js"><span class="toc-text">在 index.html中直接引用 less.js</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#less-%E7%9A%84%E7%BC%96%E8%AF%91"><span class="toc-text">less 的编译</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%AE%89%E8%A3%85-Node-js"><span class="toc-text">1、安装 Node.js</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E5%AE%89%E8%A3%85-less-%E7%9A%84%E7%BC%96%E8%AF%91%E7%8E%AF%E5%A2%83"><span class="toc-text">2、安装 less 的编译环境</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E5%B0%86-less-%E6%96%87%E4%BB%B6%E7%BC%96%E8%AF%91%E4%B8%BA-css-%E6%96%87%E4%BB%B6"><span class="toc-text">3、将 less 文件编译为 css 文件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      03-Less详解
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2009-07-31T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2009-08-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/%E7%A7%BB%E5%8A%A8web/" 
          class="post-meta-link">
          移动web
        </a>
      
    
    
      <span class="dot"></span>
      <span>2.5k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/%E7%A7%BB%E5%8A%A8web/" 
            class="post-meta-link">
            移动web
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="CSS-预处理器"><a href="#CSS-预处理器" class="headerlink" title="CSS 预处理器"></a>CSS 预处理器</h2><h3 id="为什么要有-CSS-预处理器"><a href="#为什么要有-CSS-预处理器" class="headerlink" title="为什么要有 CSS 预处理器"></a>为什么要有 CSS 预处理器</h3><p><strong>CSS基本上是设计师的工具，不是程序员的工具</strong>。在程序员的眼里，CSS是很头痛的事情，它并不像其它程序语言，比如说PHP、Javascript等等，有自己的变量、常量、条件语句以及一些编程语法，只是一行行单纯的属性描述，写起来相当的费事，而且代码难以组织和维护。</p>
<p>很自然的，有人就开始在想，能不能给CSS像其他程序语言一样，加入一些编程元素，让CSS能像其他程序语言一样可以做一些预定的处理。这样一来，就有了“<strong>CSS预处器</strong>（CSS Preprocessor）”。</p>
<h3 id="什么是-CSS-预处理器"><a href="#什么是-CSS-预处理器" class="headerlink" title="什么是 CSS 预处理器"></a>什么是 CSS 预处理器</h3><ul>
<li>是 CSS 语言的<strong>超集</strong>，比CSS更丰满。</li>
</ul>
<p>CSS 预处理器定义了一种新的语言，其基本思想是：<strong>用一种专门的编程语言，为CSS增加了一些编程的特性</strong>，将CSS作为目标生成文件，然后开发者就只要使用这种语言进行编码工作。</p>
<p>通俗的说，<strong>CSS预处理器用一种专门的编程语言，进行Web页面样式设计，然后再编译成正常的CSS文件</strong>，以供项目使用。CSS预处理器为CSS增加一些编程的特性，无需考虑浏览器的兼容性问题，例如你可以在CSS中使用变量、简单的逻辑程序、函数等等在编程语言中的一些基本特性，可以让你的CSS更加简洁、适应性更强、可读性更佳，更易于代码的维护等诸多好处。</p>
<p>CSS预处理器技术已经非常成熟，而且也涌现出了很多种不同的CSS预处理器语言，比如说：<strong>Sass（SCSS）、LESS</strong>、Stylus、Turbine、Swithch CSS、CSS Cacheer、DT CSS等。如此之多的CSS预处理器，那么“我应该选择哪种CSS预处理器？”也相应成了最近网上的一大热门话题，在Linkedin、Twitter、CSS-Trick、知乎以及各大技术论坛上，很多人为此争论不休。相比过去我们对是否应该使用CSS预处理器的话题而言，这已经是很大的进步了。</p>
<p>到目前为止，在众多优秀的CSS预处理器语言中就属<strong>Sass、LESS和Stylus最优秀</strong>，讨论的也多，对比的也多。本文将分别从他们产生的背景、安装、使用语法、异同等几个对比之处向你介绍这三款CSS预处理器语言。相信前端开发工程师会做出自己的选择——我要选择哪款CSS预处理器。</p>
<h2 id="less-的介绍"><a href="#less-的介绍" class="headerlink" title="less 的介绍"></a>less 的介绍</h2><p>less 是一款比较流行的<strong>预处理 CSS</strong>，支持变量、混合、函数、嵌套、循环等特点。</p>
<ul>
<li><p><a target="_blank" rel="noopener" href="http://lesscss.org/">官网</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="http://lesscss.cn/">中文网（less 文档）</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://less.bootcss.com/">Bootstrap网站的 less 文档</a></p>
</li>
<li><p>推荐文章：<a target="_blank" rel="noopener" href="http://www.w3cplus.com/css/less">http://www.w3cplus.com/css/less</a></p>
</li>
</ul>
<h2 id="less-的语法"><a href="#less-的语法" class="headerlink" title="less 的语法"></a>less 的语法</h2><h3 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h3><p>less 的注释可以有两种。</p>
<p>第一种注释：模板注释</p>
<pre class="highlight"><span class="line">// 模板注释 这里的注释转换成CSS后将会删除</span><br></pre>

<p>因为 less 要转换为 css才能在浏览器中使用。转换成 css 之后，这种注释会被删除（毕竟 css 不识别这种注释）。</p>
<p>第二种注释：CSS 注释语法</p>
<pre class="highlight"><span class="line"></span><br><span class="line"><span class="comment">/* CSS 注释语法 转换为CSS后让然保留 */</span></span><br></pre>

<p>总结：如果在less中写注释，我们推荐写第一种注释。除非是类似于版权等内容，就采用第二种注释。</p>
<h3 id="定义变量"><a href="#定义变量" class="headerlink" title="定义变量"></a>定义变量</h3><p>我们可以把<strong>重复使用或经常修改的值</strong>定义为变量，在需要使用的地方引用这个变量即可。这样可以避免很多重复的工作量。</p>
<p>（1）在less文件中，定义一个变量的格式：</p>
<pre class="highlight"><span class="line">@变量名: 变量值;        <span class="comment">//格式</span></span><br><span class="line"></span><br><span class="line"><span class="variable">@bgColor:</span> <span class="number">#f5f5f5</span>;      <span class="comment">//格式举例</span></span><br></pre>

<p>（2）同时，在 less 文件中引用这个变量。</p>
<p>最终，less文件的完整版代码如下：</p>
<p>main.less：</p>
<pre class="highlight"><span class="line"><span class="comment">// 定义变量</span></span><br><span class="line"><span class="variable">@bgColor:</span> <span class="number">#f5f5f5</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 引用变量</span></span><br><span class="line"><span class="selector-tag">body</span>&#123;</span><br><span class="line">    <span class="attribute">background-color</span>: <span class="variable">@bgColor</span>;</span><br><span class="line">&#125;</span><br></pre>


<p>我们将上面的less文件编译为 css 文件后（下一段讲less文件的编译），自动生成的代码如下：</p>
<p>main.css：</p>
<pre class="highlight"><span class="line"><span class="selector-tag">body</span>&#123;</span><br><span class="line">    <span class="attribute">background-color</span>: <span class="number">#f5f5f5</span>;</span><br><span class="line">&#125;</span><br></pre>


<h3 id="使用嵌套"><a href="#使用嵌套" class="headerlink" title="使用嵌套"></a>使用嵌套</h3><p>在 css 中经常会用到子代选择器，效果可能是这样的：</p>
<pre class="highlight"><span class="line"><span class="selector-class">.container</span> &#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">1024px</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.container</span> &gt; <span class="selector-class">.row</span> &#123;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100%</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.container</span> &gt; <span class="selector-class">.row</span> <span class="selector-tag">a</span> &#123;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#f40</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.container</span> &gt; <span class="selector-class">.row</span> <span class="selector-tag">a</span><span class="selector-pseudo">:hover</span> &#123;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#f50</span>;</span><br><span class="line">&#125;</span><br></pre>

<p>上面的代码嵌套了很多层，写起来很繁琐。可如果用 less 的嵌套语法来写这段代码，就比较简洁。</p>
<p>嵌套的举例如下：</p>
<p>main.less:</p>
<pre class="highlight"><span class="line"><span class="selector-class">.container</span> &#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="variable">@containerWidth</span>;</span><br><span class="line"></span><br><span class="line">  &gt; <span class="selector-class">.row</span> &#123;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">100%</span>;</span><br><span class="line">    <span class="selector-tag">a</span> &#123;</span><br><span class="line">      <span class="attribute">color</span>: <span class="number">#f40</span>;</span><br><span class="line"></span><br><span class="line">      <span class="selector-tag">&amp;</span><span class="selector-pseudo">:hover</span> &#123;</span><br><span class="line">        <span class="attribute">color</span>: <span class="number">#f50</span>;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="selector-tag">div</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">100px</span>;</span><br><span class="line"></span><br><span class="line">    <span class="selector-class">.hello</span> &#123;</span><br><span class="line">      <span class="attribute">background-color</span>: <span class="number">#00f</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre>

<p>将上面的less文件编译为 css 文件后，自动生成的代码如下：</p>
<p>main.css</p>
<pre class="highlight"><span class="line"><span class="selector-class">.container</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">1024px</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.container</span> &gt; <span class="selector-class">.row</span> &#123;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">100%</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.container</span> &gt; <span class="selector-class">.row</span> <span class="selector-tag">a</span> &#123;</span><br><span class="line">    <span class="attribute">color</span>: <span class="number">#f40</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.container</span> &gt; <span class="selector-class">.row</span> <span class="selector-tag">a</span><span class="selector-pseudo">:hover</span> &#123;</span><br><span class="line">    <span class="attribute">color</span>: <span class="number">#f50</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.container</span> <span class="selector-tag">div</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">100px</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.container</span> <span class="selector-tag">div</span> <span class="selector-class">.hello</span> &#123;</span><br><span class="line">    <span class="attribute">background-color</span>: <span class="number">#00f</span>;</span><br><span class="line">&#125;</span><br></pre>



<h3 id="Mixin"><a href="#Mixin" class="headerlink" title="Mixin"></a>Mixin</h3><p>Mixin 的作用是把<strong>重复的代码</strong>放到一个类当中，每次只要引用类名，就可以引用到里面的代码了，非常方便。</p>
<p>（1）在 less 文件中定义一个类：（将重复的代码放到自定义的类中）</p>
<pre class="highlight"><span class="line"><span class="comment">/* 定义一个类 */</span></span><br><span class="line"><span class="selector-class">.roundedCorners</span>(<span class="variable">@radius</span>: <span class="number">5px</span>) &#123;</span><br><span class="line">  -moz-<span class="attribute">border-radius</span>: <span class="variable">@radius</span>;</span><br><span class="line">  -webkit-<span class="attribute">border-radius</span>: <span class="variable">@radius</span>;</span><br><span class="line">  <span class="attribute">border-radius</span>: <span class="variable">@radius</span>;</span><br><span class="line">&#125;</span><br></pre>

<p>上方代码中，第一行里面，括号里的内容是参数：这个参数是<strong>缺省值</strong>。</p>
<p>（2）在 less 文件中引用上面这个类：</p>
<pre class="highlight"><span class="line"><span class="selector-id">#header</span> &#123;</span><br><span class="line">  <span class="selector-class">.roundedCorners</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-id">#footer</span> &#123;</span><br><span class="line">  <span class="selector-class">.roundedCorners</span>(<span class="number">10px</span>);</span><br><span class="line">&#125;</span><br></pre>



<p>上方代码中，header 中的引用没有带参数，表示参数为缺省值； footer 中的引用带了参数，那就用这个参数。</p>
<h3 id="Import"><a href="#Import" class="headerlink" title="Import"></a>Import</h3><p>在开发阶段，我们可以将不同的样式放到多个文件中，最后通过@import 的方式合并。意思就是，当出现多个 less 文件时，怎么引用它们。</p>
<p>这个很好理解， css 文件可以有很多个，less文件也可以有很多个。</p>
<p>（1）定义一个被引用的less文件，名为<code>_button1.less</code>：</p>
<p><code>_button1.less:</code></p>
<pre class="highlight"><span class="line"><span class="selector-class">.btn</span>&#123;</span><br><span class="line">  <span class="attribute">line-height</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="variable">@btnColor</span>;</span><br><span class="line">&#125;</span><br></pre>

<p>PS1：被引用的less文件，我们习惯在前面加<strong>下划线</strong>，表示它是<strong>部分文件</strong>。</p>
<p>PS2：<code>_button1.less</code>里可以引用<code>main.css</code>里的自定义变量。</p>
<p>（2）在 <code>main.css</code> 中引用上面的 <code>_button1.less</code>：（代码的第二行）</p>
<p>main.css：</p>
<pre class="highlight"><span class="line"><span class="variable">@btnColor:</span> red;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@import</span> url(<span class="string">`_button1.less:&#x27;</span>);    <span class="comment">//这里的路径写的是相对路径</span></span><br><span class="line"></span><br><span class="line"><span class="selector-tag">body</span>&#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">1024px</span>;</span><br><span class="line">&#125;</span><br></pre>

<p>将 上面的main.less 编译为 main.css之后，自动生成的代码如下：</p>
<pre class="highlight"><span class="line"><span class="selector-class">.btn</span> &#123;</span><br><span class="line">    <span class="attribute">line-height</span>: <span class="number">100px</span>;</span><br><span class="line">    <span class="attribute">color</span>: red;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">body</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">1024px</span>;</span><br><span class="line">&#125;</span><br></pre>

<h3 id="内置函数"><a href="#内置函数" class="headerlink" title="内置函数"></a>内置函数</h3><p>less 里有一些内置的函数，这里讲一下 lighten 和 darken 这两个内置函数。</p>
<p>main.less:</p>
<pre class="highlight"><span class="line"><span class="selector-tag">body</span> &#123;</span><br><span class="line">  <span class="attribute">background-color</span>: <span class="built_in">lighten</span>(<span class="number">#000</span>, <span class="number">10%</span>);   <span class="comment">// 让黑色变亮 10%</span></span><br><span class="line">  <span class="attribute">color</span>: <span class="built_in">darken</span>(<span class="number">#fff</span>, <span class="number">10%</span>);               <span class="comment">// 让白色变暗 10%</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>


<p>将 上面的 main.less 编译为 main.css 之后，自动生成的代码如下：</p>
<p>main.css：</p>
<pre class="highlight"><span class="line"><span class="selector-tag">body</span> &#123;</span><br><span class="line">  <span class="attribute">background-color</span>: <span class="number">#1a1a1a</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#e6e6e6</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>


<p>如果还有什么不懂的，可以看 api 文档，在上面的第二段附上了链接。</p>
<h2 id="在-index-html中直接引用-less-js"><a href="#在-index-html中直接引用-less-js" class="headerlink" title="在 index.html中直接引用 less.js"></a>在 index.html中直接引用 less.js</h2><ul>
<li><p>做法一：写完 less文件后，将其编译为 css 文件，然后在代码中引用css文件。</p>
</li>
<li><p>做法二：在代码中直接用引用 less 文件。</p>
</li>
</ul>
<p>产品上线后，当然是使用做法一，因为做法二会多出编译的时间。</p>
<p>平时开发或演示demo的时候可以用做法二。</p>
<p>这一段，我们讲一下做法二，其实是浏览器在本地在线地把 less 文件转换为 css 文件。</p>
<p>（1）在 less 官网下载 less.js 文件：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180226_2131.png" class="lozad post-image"src="http://img.smyhvae.com/20180226_2131.png"></p>
<p>把下载好的文件放在工程文件的lib文件夹里：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180226_2143.png" class="lozad post-image"src="http://img.smyhvae.com/20180226_2143.png"></p>
<p>（2）在 index.html 中引入 less.js 和我们自己写的  main.less。位置如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180226_2145.png" class="lozad post-image"src="http://img.smyhvae.com/20180226_2145.png"></p>
<p>copy 红框那部分的代码如下：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet/less&quot;</span> <span class="attr">href</span>=<span class="string">&quot;../main.less&quot;</span>&gt;</span></span><br></pre>

<p>我们可以在打开的网页中，通过控制台看到效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180226_2150.png" class="lozad post-image"src="http://img.smyhvae.com/20180226_2150.png"></p>
<p>注意，我们要在服务器中打开 html 文件，否则，看不到效果。</p>
<p>这里也告诉了我们：</p>
<blockquote>
<p>不提倡将 less 引入页面，因为 less 需要编译，因此你就需要再引入一个less.js, 多了一个HTTP 请求，同时当浏览器禁用了 js 你的样式就不起作用了，less 编译应该在服务端或使用 grunt 自动编译。</p>
</blockquote>
<p>工程文件：（我引用的less.js版本是 2.5.3）</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://github.com/qianguyihao/web-resource/blob/main/project/2018-02-27-LessDemo.rar">2018-02-27-LessDemo.rar</a></li>
</ul>
<p>参考链接：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://www.zhihu.com/question/26075208">知乎 | less文件如何引入页面</a></li>
</ul>
<h2 id="less-的编译"><a href="#less-的编译" class="headerlink" title="less 的编译"></a>less 的编译</h2><p>less 的编译指的是将写好的 less 文件 生成为 css 文件。</p>
<p>less 的编译，依赖于 NodeJS 环境。因此，我们需要先安装 NodeJS。</p>
<h3 id="1、安装-Node-js"><a href="#1、安装-Node-js" class="headerlink" title="1、安装 Node.js"></a>1、安装 Node.js</h3><p>去 <a target="_blank" rel="noopener" href="https://nodejs.org/zh-cn/">Node.js</a>的官网下载安装包：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180226_2153.png" class="lozad post-image"src="http://img.smyhvae.com/20180226_2153.png"></p>
<p>一路 next 进行安装。</p>
<p>安装完成后，配置环境变量：</p>
<p>在 path 变量中追加安装路径：<code>;C:\Program Files\nodejs</code>。重启资源管理器，即可生效。</p>
<p>PS：我发现，我安装的 node.js v8.9.4 版本，已经自动添加了环境变量。</p>
<p>在 cmd 命令行，输入<code>node.exe -v</code>，可以查看 node.js 的版本。</p>
<h3 id="2、安装-less-的编译环境"><a href="#2、安装-less-的编译环境" class="headerlink" title="2、安装 less 的编译环境"></a>2、安装 less 的编译环境</h3><p>将 <a target="_blank" rel="noopener" href="http://download.csdn.net/download/smyhvae/10260414">npm.zip</a> 解压，将解压后的文件拷贝到路径<code>C:\Users\smyhvae\AppData\Roaming\npm</code>下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180226_2212.png" class="lozad post-image"src="http://img.smyhvae.com/20180226_2212.png"></p>
<p>然后重启资源管理器（或者重启电脑）。在 cmd 中输入 <code>lessc</code>，如果能看到下面的效果，说明 less编译环境安装成功：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180226_2217.png" class="lozad post-image"src="http://img.smyhvae.com/20180226_2217.png"></p>
<p>如果你用的是 linux 系统，可以输入下面的命令安装：</p>
<pre class="highlight"><span class="line">npm install -g less</span><br></pre>

<h3 id="3、将-less-文件编译为-css-文件"><a href="#3、将-less-文件编译为-css-文件" class="headerlink" title="3、将 less 文件编译为 css 文件"></a>3、将 less 文件编译为 css 文件</h3><p>在 less 所在的路径下，输入 <code>lessc xxx.less</code>，即可编译成功。或者，如果输入 <code>lessc xxx.less &gt; ..\xx.css</code>，表示输出到指定路径。</p>
<h2 id="我的公众号"><a href="#我的公众号" class="headerlink" title="我的公众号"></a>我的公众号</h2><p>想学习<font color=#0000ff><strong>更多技能</strong></font>？不妨关注我的微信公众号：<strong>千古壹号</strong>（id：<code>qianguyihao</code>）。</p>
<p>扫一扫，你将发现另一个全新的世界，而这将是一场美丽的意外：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2016040102.jpg" class="lozad post-image"src="http://img.smyhvae.com/2016040102.jpg"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2009/08/01/09-%E7%A7%BB%E5%8A%A8Web%E5%BC%80%E5%8F%91/03-Less%E8%AF%A6%E8%A7%A3/">
            https://zwl5670.gitee.io/2009/08/01/09-%E7%A7%BB%E5%8A%A8Web%E5%BC%80%E5%8F%91/03-Less%E8%AF%A6%E8%A7%A3/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2009/08/01/09-%E7%A7%BB%E5%8A%A8Web%E5%BC%80%E5%8F%91/01-Bootstrap%E5%85%A5%E9%97%A8/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">01-Bootstrap入门 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2008/08/01/08-%E5%89%8D%E7%AB%AF%E5%9F%BA%E6%9C%AC%E5%8A%9F%EF%BC%9ACSS%E5%92%8CDOM%E7%BB%83%E4%B9%A0/01-CSS%E5%9F%BA%E7%A1%80%E7%BB%83%E4%B9%A0%EF%BC%9AJD%E9%A6%96%E9%A1%B5%E7%9A%84%E5%88%B6%E4%BD%9C%EF%BC%88%E9%A1%B6%E9%83%A8%E5%92%8C%E5%BA%95%E9%83%A8%EF%BC%89/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">01-CSS基础练习：JD首页的制作（顶部和底部） </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E9%A2%84%E5%A4%84%E7%90%86%E5%99%A8"><span class="toc-text">CSS 预处理器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E6%9C%89-CSS-%E9%A2%84%E5%A4%84%E7%90%86%E5%99%A8"><span class="toc-text">为什么要有 CSS 预处理器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF-CSS-%E9%A2%84%E5%A4%84%E7%90%86%E5%99%A8"><span class="toc-text">什么是 CSS 预处理器</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#less-%E7%9A%84%E4%BB%8B%E7%BB%8D"><span class="toc-text">less 的介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#less-%E7%9A%84%E8%AF%AD%E6%B3%95"><span class="toc-text">less 的语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E9%87%8A"><span class="toc-text">注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F"><span class="toc-text">定义变量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%B5%8C%E5%A5%97"><span class="toc-text">使用嵌套</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mixin"><span class="toc-text">Mixin</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Import"><span class="toc-text">Import</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0"><span class="toc-text">内置函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8-index-html%E4%B8%AD%E7%9B%B4%E6%8E%A5%E5%BC%95%E7%94%A8-less-js"><span class="toc-text">在 index.html中直接引用 less.js</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#less-%E7%9A%84%E7%BC%96%E8%AF%91"><span class="toc-text">less 的编译</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%AE%89%E8%A3%85-Node-js"><span class="toc-text">1、安装 Node.js</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E5%AE%89%E8%A3%85-less-%E7%9A%84%E7%BC%96%E8%AF%91%E7%8E%AF%E5%A2%83"><span class="toc-text">2、安装 less 的编译环境</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E5%B0%86-less-%E6%96%87%E4%BB%B6%E7%BC%96%E8%AF%91%E4%B8%BA-css-%E6%96%87%E4%BB%B6"><span class="toc-text">3、将 less 文件编译为 css 文件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-%E9%A2%84%E5%A4%84%E7%90%86%E5%99%A8"><span class="toc-text">CSS 预处理器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E6%9C%89-CSS-%E9%A2%84%E5%A4%84%E7%90%86%E5%99%A8"><span class="toc-text">为什么要有 CSS 预处理器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF-CSS-%E9%A2%84%E5%A4%84%E7%90%86%E5%99%A8"><span class="toc-text">什么是 CSS 预处理器</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#less-%E7%9A%84%E4%BB%8B%E7%BB%8D"><span class="toc-text">less 的介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#less-%E7%9A%84%E8%AF%AD%E6%B3%95"><span class="toc-text">less 的语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E9%87%8A"><span class="toc-text">注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F"><span class="toc-text">定义变量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%B5%8C%E5%A5%97"><span class="toc-text">使用嵌套</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mixin"><span class="toc-text">Mixin</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Import"><span class="toc-text">Import</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0"><span class="toc-text">内置函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8-index-html%E4%B8%AD%E7%9B%B4%E6%8E%A5%E5%BC%95%E7%94%A8-less-js"><span class="toc-text">在 index.html中直接引用 less.js</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#less-%E7%9A%84%E7%BC%96%E8%AF%91"><span class="toc-text">less 的编译</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%AE%89%E8%A3%85-Node-js"><span class="toc-text">1、安装 Node.js</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81%E5%AE%89%E8%A3%85-less-%E7%9A%84%E7%BC%96%E8%AF%91%E7%8E%AF%E5%A2%83"><span class="toc-text">2、安装 less 的编译环境</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E5%B0%86-less-%E6%96%87%E4%BB%B6%E7%BC%96%E8%AF%91%E4%B8%BA-css-%E6%96%87%E4%BB%B6"><span class="toc-text">3、将 less 文件编译为 css 文件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
